﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Styx.TreeSharp;
using Action = Styx.TreeSharp.Action;
using Styx.WoWInternals;
using Styx;
using Styx.CommonBot;
using Styx.Pathing;
using Styx.Common;
using System.Windows.Forms;
using Styx.CommonBot.Routines;
using System.Windows.Media;
using Bots.DungeonBuddy.Helpers;
using Styx.CommonBot.POI;
using Styx.WoWInternals;
using System.Runtime.InteropServices;

namespace DestroLock
{
    public static class EventHandlers
    {
        public static bool PauseAoe = false;
        public static bool Paused = false;
        public static bool SwitchBearForm = false;

        
        #region GetAsyncKeyState

        [DllImport("user32.dll")]
        private static extern short GetAsyncKeyState(Keys vKey);

        #endregion
     
        public static void CombatLogErrorHandler(object sender, LuaEventArgs args)
        {

            foreach (object arg in args.Args)
            {

                var s = (string)arg;

                Logging.Write(Colors.Red, "Error message = " + s.ToUpper());
                string errorLog = s.ToUpper();

                switch (errorLog)
                {
                    case "IMMUNE":
                        Lua.DoString("StopAttack()");
                        StyxWoW.Me.ClearTarget();
                        break;
                    case "OUT OF RANGE" :
                        if (StyxWoW.Me.CurrentTarget != null && StyxWoW.Me.CurrentTarget.Distance > 5)
                        {
                            while (StyxWoW.Me.CurrentTarget != null && StyxWoW.Me.CurrentTarget.Distance > 5)
                            {
                                Navigator.MoveTo(StyxWoW.Me.CurrentTarget.Location);
                            }
                            Navigator.PlayerMover.MoveStop();
                        }
                        break;
                    case "TARGET NOT IN LINE OF SIGHT":
                        Lua.DoString("StopAttack()");
                        if (StyxWoW.Me.CurrentTarget != null && StyxWoW.Me.CurrentTarget.Distance > 5)
                        {
                            while (StyxWoW.Me.CurrentTarget != null && StyxWoW.Me.CurrentTarget.Distance > 5)
                            {
                                Navigator.MoveTo(StyxWoW.Me.CurrentTarget.Location);
                            }
                            Navigator.PlayerMover.MoveStop();
                        }
                        break;
                    case "INVALID TARGET":
                        Logging.Write("My target is not valid.");
                        Logging.Write("Blacklisting for 1 min");
                        Lua.DoString("StopAttack()");
                        StyxWoW.Me.ClearTarget();
                        Blacklist.Add(StyxWoW.Me.CurrentTargetGuid, TimeSpan.FromMinutes(1));
                        break;
                    default:
                        return;
                }


            }
        }
        #region pause

        public static void HandleModifierStateChanged(object sender, LuaEventArgs args)
        {
            if (LockSettings.myPrefs.PauseKeys == LockSettings.Keypress.None
                && LockSettings.myPrefs.AoePauseKeys == LockSettings.Keypress.None)
            {
                return;
            }
            if (Convert.ToInt32(args.Args[1]) == 1)
            {
                string pauseMyAoe = "AOE cancelled, press " + LockSettings.myPrefs.PauseKeys.ToString() + " in WOW again to turn AOE back on";
                if (args.Args[0].ToString() == LockSettings.myPrefs.AoePauseKeys.ToString())
                {
                    PauseAoe = !PauseAoe;
                    if (PauseAoe)
                    {
                        Logging.Write("Aoe cancelled, press {0} in WOW again to resume Aoe",
                                     LockSettings.myPrefs.AoePauseKeys.ToString());
                        if (LockSettings.myPrefs.PrintMsg)
                            Lua.DoString(
                                "RaidNotice_AddMessage(RaidWarningFrame, \"" + pauseMyAoe + "\", ChatTypeInfo[\"RAID_WARNING\"]);");
                    }
                    else
                    {
                        Logging.Write("Aoe Resumed");
                        if (LockSettings.myPrefs.PrintMsg)
                            Lua.DoString(
                                "RaidNotice_AddMessage(RaidWarningFrame, \"Aoe Resumed !\", ChatTypeInfo[\"RAID_WARNING\"]);");
                    }
                }
            }
            if (Convert.ToInt32(args.Args[1]) == 1)
            {
                if (args.Args[0].ToString() == LockSettings.myPrefs.PauseKeys.ToString())
                {
                    string pauseCR = "CR paused, press " + LockSettings.myPrefs.PauseKeys.ToString() + " in WOW again to turn back on";
                    Paused = !Paused;
                    if (Paused)
                    {
                        Logging.Write("CR paused, press {0} in WOW again to turn back on",
                                     LockSettings.myPrefs.PauseKeys.ToString());
                        if (LockSettings.myPrefs.PrintMsg)
                            Lua.DoString(
                                "RaidNotice_AddMessage(RaidWarningFrame, \"" + pauseCR + "\", ChatTypeInfo[\"RAID_WARNING\"]);");
                    }
                    else
                    {
                        Logging.Write("CR running again ....");
                        if (LockSettings.myPrefs.PrintMsg)
                            Lua.DoString(
                                "RaidNotice_AddMessage(RaidWarningFrame, \"CR running again ....\", ChatTypeInfo[\"RAID_WARNING\"]);");
                    }
                }
            }
        }
        #endregion pause

        #region goes into main
        /* #region Initialize
        public override void Initialize()
        {
            Lua.Events.AttachEvent("MODIFIER_STATE_CHANGED", HandleModifierStateChanged);
            Lua.Events.AttachEvent("UI_ERROR_MESSAGE", CombatLogErrorHandler);
            EventHandlers.Init();
        }
        #endregion Initialize

        #region Shutsown
        public override void ShutDown()
        {
            Lua.Events.DetachEvent("MODIFIER_STATE_CHANGED", HandleModifierStateChanged);
            Lua.Events.DetachEvent("UI_ERROR_MESSAGE", CombatLogErrorHandler);
        }
        #endregion shutdown */
        #endregion goes into main
    }
}
